package com.huayun.viewutils.iconview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.google.gson.Gson;
import com.huayun.util.utils.Utils;
import com.huayun.viewutils.R;


/**
 * Description:
 * Created on 2019/8/19 0019 14:31:12
 * Organization:华云
 * author:AHuangSHang
 */

public class IconFontView extends AppCompatTextView {

    private String text, code, color, secondColor, iconType;
    private boolean setSecondColor;
    private boolean onDraw = true;//是否重绘
    private int normalColor;

    public String getIconType() {
        return iconType;
    }

    /**
     * 设置字体图标
     *
     * @param icon_type
     */
    public void setIconType(String icon_type) {
        this.iconType = icon_type;
        setSecondColor = false;
        setView();
        invalidate();
    }

    /**
     * 设置字体图标显示第二个颜色
     *
     * @param icon_type
     */
    public void setIconTypeSecondColor(String icon_type) {
        this.iconType = icon_type;
        setSecondColor = true;
        setView();
    }

    private int size;

    public IconFontView(Context context) {
        super(context);
        init(context, null);
    }

    public IconFontView(final Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public IconFontView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (onDraw) {
            setData();
        }
    }

    IconFont icon;

    private void setView() {
        if (hasIcon()) {
            icon = new Gson().fromJson(ParseResourceUtil.getInstance().iconData.get(iconType).toString(), IconFont.class);
            code = icon.getUnicode();
            color = icon.getColor();
            size = icon.getSize();
            secondColor = icon.getSecondColor();
            setData();
        } else {
            setText(iconType);
            if (this.isPressed()) {
                setTextColor(getResources().getColor(R.color.lightFontColor));
            } else {
                setTextColor(normalColor != 0 ? normalColor : getResources().getColor(R.color.titleIconColor));
            }
        }
    }

    private boolean hasIcon() {
        return Utils.notNull(iconType) && ParseResourceUtil.getInstance().iconData.containsKey(iconType);
    }

    private void setData() {
        if (hasIcon()) {
            if (Utils.notNull(code)) {
                text = code.toLowerCase();
                StringBuffer stringBuffer = new StringBuffer("&#x");
                String string = stringBuffer.append(text.substring(2, 6)).append(";").toString();
                this.setText(Html.fromHtml(string));
                this.setTypeface(ParseResourceUtil.getInstance().typeface);
                this.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
                if (setSecondColor) {
                    if (Utils.notNull(secondColor)) {
                        this.setTextColor(Color.parseColor(secondColor));
                    }
                } else {
                    if (this.isPressed()) {
                        if (Utils.notNull(secondColor)) {
                            this.setTextColor(Color.parseColor(secondColor));
                        }
                    } else {
                        this.setTextColor(Color.parseColor(color));
                    }
                }
            } else {
                text = "";
                this.setText(text);
            }
        } else {
            setText(iconType);
            if (this.isPressed()) {
                setTextColor(getResources().getColor(R.color.lightFontColor));
            } else {
                setTextColor(normalColor != 0 ? normalColor : getResources().getColor(R.color.titleIconColor));
            }
        }
    }

    public void init(Context context, AttributeSet attrs) {
        onDraw = true;
        if (isInEditMode()) {
            //do something
            //造成 error code
            return;
        }
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconFontView);//TypedArray是一个数组容器
            boolean b = a.getBoolean(R.styleable.IconFontView_clickable, true);
            String iconType = a.getString(R.styleable.IconFontView_icontype);
            setIconType(iconType);
            this.setClickable(b);
            a.recycle();
        }
        setText(" ");
        setView();
    }

    /**
     * 设置字体图标的颜色
     *
     * @param color
     */
    public void setIconColor(int color) {
        setTextColor(color);
        onDraw = false;
    }

    /**
     * 设置字体图标的颜色
     *
     * @param color
     */
    public void setIconTwoColor(int color) {
        normalColor = color;
        onDraw = true;
    }

    /**
     * 设置字体图标的大小
     *
     * @param size 单位为sp
     */
    public void setIconSizeSP(int size) {
        this.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        onDraw = false;
    }

    /**
     * 设置字体图标的大小
     *
     * @param size 单位为dp
     */
    public void setIconSizeDP(int size) {
        this.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
        onDraw = false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
